a11y: Set an accessible role for GtkPasswordEntry
authorMatthias Clasen <mclasen@redhat.com>
Tue, 28 Jul 2020 22:23:57 +0000 (18:23 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Tue, 28 Jul 2020 22:23:57 +0000 (18:23 -0400)
Use the text-box accessible role for GtkPasswordEntry.
And set properties as appropriate.

Update the documentation and add a test.

docs/reference/gtk/section-accessibility.md
gtk/gtkpasswordentry.c
testsuite/a11y/meson.build
testsuite/a11y/passwordentry.c [new file with mode: 0644]

index 30c6f9e42b552d3239be95574b056c624168a94d..1885b928f82ed36227dc631237dd3302f359d12e 100644 (file)
@@ -60,7 +60,7 @@ Each role name is part of the #GtkAccessibleRole enumeration.
 | `SEPARATOR` | A divider that separates sections of content or groups of items | #GtkSeparator |
 | `SPIN_BUTTON` | A range control that allows seelcting among discrete choices | #GtkSpinButton |
 | `SWITCH` | A control that represents on/off values | #GtkSwitch |
-| `TEXT_BOX` | A type of input that allows free-form text as its value. | #GtkEntry |
+| `TEXT_BOX` | A type of input that allows free-form text as its value. | #GtkEntry, #GtkPasswordEntry |
 | `WINDOW` | An application window | #GtkWindow |
 | `...` | … |
 
index 93d560e49e35518abcf7ade152d1af3098d69062..252b8be0f2fe851dcc670861bf3415262635ffe6 100644 (file)
  * a .passwordstyle class. The text Css node below it has a child with
  * name image and style class .caps-lock-indicator for the Caps Lock
  * icon, and possibly other children.
+ *
+ * # Accessibility
+ *
+ * GtkPasswordEntry uses the #GTK_ACCESSIBLE_ROLE_TEXT_BOX role.
  */
 
 typedef struct {
@@ -222,14 +226,27 @@ gtk_password_entry_set_property (GObject      *object,
 {
   GtkPasswordEntry *entry = GTK_PASSWORD_ENTRY (object);
   GtkPasswordEntryPrivate *priv = gtk_password_entry_get_instance_private (entry);
+  const char *text;
 
   if (gtk_editable_delegate_set_property (object, prop_id, value, pspec))
-    return;
+    {
+      if (prop_id == NUM_PROPERTIES + GTK_EDITABLE_PROP_EDITABLE)
+        {
+          gtk_accessible_update_property (GTK_ACCESSIBLE (entry),
+                                          GTK_ACCESSIBLE_PROPERTY_READ_ONLY, !g_value_get_boolean (value),
+                                          -1);
+        }
+      return;
+    }
 
   switch (prop_id)
     {
     case PROP_PLACEHOLDER_TEXT:
-      gtk_text_set_placeholder_text (GTK_TEXT (priv->entry), g_value_get_string (value));
+      text = g_value_get_string (value);
+      gtk_text_set_placeholder_text (GTK_TEXT (priv->entry), text);
+      gtk_accessible_update_property (GTK_ACCESSIBLE (entry),
+                                      GTK_ACCESSIBLE_PROPERTY_PLACEHOLDER, text,
+                                      -1);
       break;
 
     case PROP_ACTIVATES_DEFAULT:
@@ -424,6 +441,7 @@ gtk_password_entry_class_init (GtkPasswordEntryClass *klass)
   gtk_editable_install_properties (object_class, NUM_PROPERTIES);
 
   gtk_widget_class_set_css_name (widget_class, I_("entry"));
+  gtk_widget_class_set_accessible_role (widget_class, GTK_ACCESSIBLE_ROLE_TEXT_BOX);
 }
 
 static GtkEditable *
index 8358d7b2c547c8711cf23d460c3588bd0a9d8f7f..2d828e36bdf323920f6db2a030fdf37200fc4ebc 100644 (file)
@@ -17,6 +17,7 @@ tests = [
   { 'name': 'entry' },
   { 'name': 'image' },
   { 'name': 'label' },
+  { 'name': 'passwordentry' },
   { 'name': 'progressbar' },
   { 'name': 'scrollbar' },
   { 'name': 'searchentry' },
diff --git a/testsuite/a11y/passwordentry.c b/testsuite/a11y/passwordentry.c
new file mode 100644 (file)
index 0000000..4c1b794
--- /dev/null
@@ -0,0 +1,41 @@
+#include <gtk/gtk.h>
+
+static void
+password_entry_role (void)
+{
+  GtkWidget *widget = gtk_password_entry_new ();
+  g_object_ref_sink (widget);
+
+  gtk_test_accessible_assert_role (widget, GTK_ACCESSIBLE_ROLE_TEXT_BOX);
+
+  g_object_unref (widget);
+}
+
+static void
+password_entry_properties (void)
+{
+  GtkWidget *widget = gtk_password_entry_new ();
+  g_object_ref_sink (widget);
+
+  gtk_test_accessible_assert_property (widget, GTK_ACCESSIBLE_PROPERTY_PLACEHOLDER, NULL);
+  gtk_test_accessible_assert_property (widget, GTK_ACCESSIBLE_PROPERTY_READ_ONLY, FALSE);
+
+  g_object_set (widget, "placeholder-text", "Hello", NULL);
+  gtk_editable_set_editable (GTK_EDITABLE (widget), FALSE);
+
+  gtk_test_accessible_assert_property (widget, GTK_ACCESSIBLE_PROPERTY_PLACEHOLDER, "Hello");
+  gtk_test_accessible_assert_property (widget, GTK_ACCESSIBLE_PROPERTY_READ_ONLY, TRUE);
+
+  g_object_unref (widget);
+}
+
+int
+main (int argc, char *argv[])
+{
+  gtk_test_init (&argc, &argv, NULL);
+
+  g_test_add_func ("/a11y/passwordentry/role", password_entry_role);
+  g_test_add_func ("/a11y/passwordentry/properties", password_entry_properties);
+
+  return g_test_run ();
+}